<?php

namespace App\Admin\Controllers;


use App\Models\BalanceRecord;
use App\Models\User;
use App\Models\Withdraw;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\MessageBag;

class WithdrawController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('提现记录');
            $content->description('当前为人工提现，发红包的方式到用户');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('提现记录');
            $content->description('当前为人工提现，发红包的方式到用户');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('提现记录');
            $content->description('当前为人工提现，发红包的方式到用户');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Withdraw::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                if($actions->row->withdraw_status!=0){
                    $actions->disableEdit();
                }
                $actions->disableDelete();
            });
            $grid->id('ID')->sortable();
            $grid->user_id('用户id');
            $grid->amount('提现数量');
            $grid->withdraw_status('处理结果')->display(function ($data){
                switch ($data){
                    case 0:return '待处理';
                    case 1:return '成功';
                    case 2:return '失败';
                }
            });
            $grid->wx('提现到微信号');
            $grid->code('提现码');
            $grid->img('图片')->display(function ($data) {
                if (!$data) {
                    return '';
                }
                return <<<DATA
<img src="/uploads/{$data}" 
style="max-width:50px;max-height:200px" class="img img-thumbnail">
DATA;
            });
            $grid->fail_info('失败原因');
            $grid->manager_id('操作员id');
            $grid->created_at('创建时间');
            $grid->updated_at('更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Withdraw::class, function (Form $form) {
            $form->radio('withdraw_status','处理结果')->options(['0'=>'待处理','1'=> '成功','2'=>'失败']);
            $form->text('fail_info','失败原因')->default(' ');
            // 使用随机生成文件名 (md5(uniqid()).extension)
            $image=$form->image('img','提现截图');
            $image->move('withdraws');
            $image->uniqueName();

            $form->saving(function (Form $form){
                if($form->model()->withdraw_status!=0){
                    //不是待处理状态，则抛出错误
                    $error = new MessageBag([
                        'title'   => '错误提示',
                        'message' => '非法操作',
                    ]);

                    return back()->with(compact('error'));
                }
                $manager_id=Admin::user()->id;
                $tempArr=explode(',',$form->model()->manager_id);
                if(!in_array($manager_id,$tempArr)){
                    $tempArr[]=$manager_id;
                }
                $form->model()->manager_id=implode(',',$tempArr);
            });
            $form->saved(function (Form $form) {
                if($form->model()->withdraw_status==2){
                    //失败则回退金额
                    DB::transaction(function () use($form){
                        DB::table('users')->where('id',$form->model()->user_id)->increment('balance',$form->model()->amount);
                        BalanceRecord::create([
                            'user_id'=>$form->model()->user_id,
                            'amount'=>$form->model()->amount,
                            'description'=>"提现申请失败，余额退回",
                            'action'=>'-']);

                    });

                }
            });
        });
    }
}
